<div class="container-fluid">

    <h2 class="sub-header pull-right col-md-12">
        <div class=" pull-right col-md-4 col-md-offset-8">
            <md-button class="md-raised md-primary" ng-click="$ctrl.directSave()">保存</md-button>
            <md-button class="md-raised md-primary" ng-click="$ctrl.startSave()">另存为</md-button>
        </div>
    </h2>
</div>

<div class="table-responsive">
    <table class="table table-striped">
        <thead>
            <tr>
                <th>字段</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="m in $ctrl.meta">
                <td>{{m.className}}</td>
                <td><a class="btn btn-success" ng-click="$ctrl.showDialog(m,'myModalContent.html')">基础授权</a>
                    <a class="btn btn-success" ng-click="$ctrl.showDialog(m,'exprModalContent.html')">数据过滤</a>
                    <a class="btn btn-success" ng-click="">条件表达式</a></td>
            </tr>
        </tbody>
    </table>
    <script type="text/ng-template" id="myModalContent.html">
        <div class="modal-header">
            <h3 class="modal-title" id="modal-title">授权设置</h3>
        </div>
        <div class="modal-body" id="modal-body">
            <div>{{entity.className}}</div>
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>字段</th>
                        <th ng-repeat="r in roles">{{r.name}}
                            <md-checkbox ng-change="$ctrl.batch(data[r.id].accessMap,'readable',$ctrl.model[r.id].read,entity.fieldList)" ng-model="$ctrl.model[r.id].read">
                                读
                            </md-checkbox>
                            <md-checkbox ng-change="$ctrl.batch(data[r.id].accessMap,'writable',$ctrl.model[r.id].write,entity.fieldList)" ng-model="$ctrl.model[r.id].write">
                                写
                            </md-checkbox>
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <tr title="该角色能够对数据进行的除读取和更新外其他操作">
                        <td><label>操作</label></td>
                        <td ng-repeat="r in roles">
                            <md-checkbox ng-model="data[r.id].canCreate">
                                新增
                            </md-checkbox>
                            <md-checkbox ng-model="data[r.id].canRemove">
                                删除
                            </md-checkbox>
                        </td>
                    </tr>
                    <tr ng-repeat="f in entity.fieldList" title="对字段{{f}}的查询和更新权限，写入同时控制对字段的新增和更新">
                        <td><label>{{f}}</label></td>
                        <td ng-repeat="r in roles">
                            <md-checkbox ng-model="data[r.id].accessMap[f].readable">
                                读取
                            </md-checkbox>
                            <md-checkbox ng-model="data[r.id].accessMap[f].writable">
                                写入
                            </md-checkbox>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
        <div class="modal-footer">
            <button class="btn btn-primary" type="button" ng-click="$ctrl.ok(data)">确定</button>
            <button class="btn btn-warning" type="button" ng-click="$ctrl.cancel()">取消</button>
        </div>
    </script>
    <script type="text/ng-template" id="exprModalContent.html">
        <div class="modal-header">
            <h3 class="modal-title" id="modal-title">表达式设置</h3>
        </div>
        <div class="modal-body" id="modal-body">
            <div>{{entity.className}}</div>
            <form>
                <div class="form-group"><label>角色</label>
                    <select class="form-control" ng-model="targetRole" ng-options="r.id as r.name for r in roles"></select></div>
                <div class="form-group"><label>字段</label> <select class="form-control" ng-model="new.field" ng-options="f for f in entity.fieldList"></select></div>
                <div class="form-group"><label>操作符</label><input class="form-control" ng-model="new.operator"></div>
                <div class="form-group"><label>值</label><input class="form-control" ng-model="new.valueExpr"></div>
                <a class="btn btn-primary btn-sm" ng-click="data[targetRole].filters=(data[targetRole].filters||[]);data[targetRole].filters.push(new)">添加</a>
            </form>
            <table class="table table-striped" ng-repeat="r in roles">
                <thead>
                    <tr>
                        <th colspan="3">{{r.name}}</th>
                    </tr>
                    <tr>
                        <th>字段</th>
                        <th>操作符</th>
                        <th>值</th>
                    </tr>
                </thead>
                <tbody>
                    <tr ng-repeat="f in data[r.id].filters">
                        <td><input ng-model="f.field" type="text"></td>
                        <td><input ng-model="f.operator" type="text"></td>
                        <td><input ng-model="f.valueExpr" type="text"> </td>
                    </tr>
                </tbody>
            </table>
        </div>
        <div class="modal-footer">
            <button class="btn btn-primary" type="button" ng-click="$ctrl.ok(data)">确定</button>
            <button class="btn btn-warning" type="button" ng-click="$ctrl.cancel()">取消</button>
        </div>
    </script>
</div>